﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Mario.Characters;
using Mario.Animation;
namespace Mario.Structures
{
    class Coin
    {
        //Animation
        private Animation.Animation idleAnimation;
        private SpriteEffects flip = SpriteEffects.None;
        private Animation.AnimationPlayer sprite;

        private Texture2D texture;
        private Vector2 origin;
        private SoundEffect collectedSound;
        public readonly int PointValue;
        public bool IsPowerUp { get; private set; }
        public readonly Color Color;

        //public const int PointValue = 30;
        //public readonly Color Color = Color.Yellow;
        private Vector2 basePosition;
        private float bounce;
        public Level Level
        {
            get { return level; }
        }
        Level level;
        public Vector2 Position
        {
            get
            {
                return basePosition + new Vector2(0.0f, bounce);
            }
        }
        public Circle BoundingCircle
        {
            get
            {
                return new Circle(Position, Tile.Width / 2.0f);
            }
        }
        public Coin(Level level, Vector2 position, bool isPowerUp)
        {
            this.level = level;
            this.basePosition = position;
            IsPowerUp = isPowerUp;
            if (IsPowerUp)
            {
                PointValue = 100;
                Color = Color.Red;
            }
            else
            {
                PointValue = 30;
                Color = Color.Yellow;
            }

            LoadContent();
        }
        public void LoadContent()
        {
            idleAnimation = new Animation.Animation(Level.Content.Load<Texture2D>("Sprites/GCoin"), 0.1f, true);
            origin = new Vector2(idleAnimation.FrameWidth / 2.0f, idleAnimation.FrameHeight / 2.0f);
            collectedSound = Level.Content.Load<SoundEffect>("Sounds/coin");
        }
        public void Update(GameTime gameTime)
        {
            // Bounce control constants
            const float BounceHeight = 0.18f;
            const float BounceRate = 3.0f;
            const float BounceSync = -0.75f;

            // Bounce along a sine curve over time.
            // Include the X coordinate so that neighboring gems bounce in a nice wave pattern.            
            double t = gameTime.TotalGameTime.TotalSeconds * BounceRate + Position.X * BounceSync;
            //bounce = (float)Math.Sin(t) * BounceHeight * texture.Height;
            sprite.PlayAnimation(idleAnimation);
        }
        public void OnCollected(Player collectedBy)
        {
            collectedSound.Play();
            if (IsPowerUp)
                collectedBy.PowerUp();

        }
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            sprite.Draw(gameTime, spriteBatch, Position, flip);
            //spriteBatch.Draw(texture, Position, null, Color, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f);
   
        }
    }
}
